package com.dd.cloud.user.service.phar.pharservice;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.dd.cloud.user.DTO.CallbackPresStatusReq;
import com.dd.cloud.user.DTO.CancelPresReq;
import com.dd.cloud.user.DTO.CommonParamReq;
import com.dd.cloud.common.constants.PresConstants;
import com.dd.cloud.user.entity.doctor.PatientMedCard;
import com.dd.cloud.user.entity.order.OrderPay;
import com.dd.cloud.user.entity.pres.PresOtherDetail;
import com.dd.cloud.user.entity.pres.PresPrescription;
import com.dd.cloud.common.interceptor.UserInterceptor;
import com.dd.cloud.user.req.pres.PresCreateOtherReq;
import com.dd.cloud.user.req.pres.PresCreateReq;
import com.dd.cloud.user.req.pres.PresPatientAddrReq;
import com.dd.cloud.user.service.log.ILogErrorService;
import com.dd.cloud.user.service.pres.IPresOtherDetailService;
import com.dd.cloud.user.service.pres.IPresPrescriptionService;
import com.dd.cloud.common.vo.LoginInfo;
import com.dd.cloud.common.enums.ZkExceptionEnum;
import com.dd.cloud.common.exception.ZekeException;
import com.dd.cloud.common.utils.CalendarUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * 康美药房服务类
 *
 * @author Lx
 * @version 1.0
 */
@Slf4j
@Service
public class OtherPharService extends PharAbstractService {
    @Autowired
    private IPresOtherDetailService presOtherDetailService;
    @Autowired
    private ILogErrorService logErrorService;
    @Autowired
    private IPresPrescriptionService presPrescriptionService;

    public static String replace(String s) {
        if (null != s && s.indexOf(".") > 0) {
            //去掉多余的0
            s = s.replaceAll("0+?$", "");
            //如最后一位是.则去掉
            s = s.replaceAll("[.]$", "");
        }
        return s;
    }

    /**
     * 添加药房的处方详情
     *
     * @param req
     * @param medCard
     */
    @Override
    public void addPresDetail(PresCreateReq req, PatientMedCard medCard) {
        LoginInfo loginInfo = UserInterceptor.getLoginUserInfo().get();
        PresCreateOtherReq otherReq = (PresCreateOtherReq) req;
        PresOtherDetail otherDetail = new PresOtherDetail();
        BeanUtils.copyProperties(otherReq, otherDetail);
        BeanUtils.copyProperties(medCard, otherDetail);
        otherDetail.setIsSendHos(1);
        otherDetail.setPatientName(medCard.getName());
        otherDetail.setRecipelInstru(otherReq.getDisease());
        otherDetail.setIsHos(0);
        otherDetail.setPatientId(medCard.getMedId());
        otherDetail.setIsPregnant(medCard.getPregnant() == 1 ? 0 : 1);
        otherDetail.setSex("男".equals(medCard.getSex()) ? 0 : 1);
        Integer age = CalendarUtil.getAge(medCard.getBirthDate());
        otherDetail.setAge(age == null ? null : String.valueOf(age));
        otherDetail.setDoctor(loginInfo.getName());
        otherDetail.setPatientTel(medCard.getMobileNumber());
        otherDetail.setPerPackNum(req.getDoseNum());
        otherDetail.setPerPackDose(req.getDrugDose());
        otherDetail.setTencode(req.getTencode());
        otherDetail.setCreateDate(LocalDateTime.now());
        presOtherDetailService.save(otherDetail);
    }

    @Override
    public boolean sendPresToPhar(OrderPay pay) {
        log.info("发送处方到其他药房");
        PresPrescription pres = new PresPrescription();
        pres.setTencode(pay.getServerNo());
        pres.setStatus(PresConstants.PresStatus.SENDPHARM);
        pres.setUpdateDate(LocalDateTime.now());
        boolean flag = presPrescriptionService.updateById(pres);
        if (!flag) {
            logErrorService.presBackLog("发送处方到其他药房成功, 处方更新失败", "处方发送到其他药房异常", "jsonBody=" + pres.toString());
        }
        return true;
    }

    @Override
    public void cancelPres(CancelPresReq req) {
        PresPrescription presPrescription = new PresPrescription();
        presPrescription.setTencode(req.getTencode());
        presPrescription.setStatus(PresConstants.PresStatus.CANCEL);
        presPrescription.setUpdateDate(LocalDateTime.now());
        boolean flag = presPrescriptionService.updateById(presPrescription);
        if (!flag) {
            logErrorService.presBackLog("取消其他药房处方,数据修改失败", "取消处方", "tencode=" + presPrescription.getTencode());
            throw new ZekeException(ZkExceptionEnum.CANCEL_PRES_ERROR, "数据修改失败,请联系管理员");
        }
    }

    @Override
    public String presCallback(CallbackPresStatusReq req) {
        return "success";
    }

    @Override
    public String expressCallback(CommonParamReq req) {
        return "success";
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updatePresPatientAddrInfo(PresPatientAddrReq req) {
        UpdateWrapper<PresOtherDetail> uw = new UpdateWrapper<>();
        uw.lambda().eq(PresOtherDetail::getTencode, req.getTencode());
        uw.lambda().set(PresOtherDetail::getConsigneeName, req.getReceiver());
        uw.lambda().set(PresOtherDetail::getConsigneePhone, req.getMobileNumber());
        uw.lambda().set(PresOtherDetail::getConsigneeProvince, req.getProvince());
        uw.lambda().set(PresOtherDetail::getConsigneeCity, req.getCity());
        uw.lambda().set(PresOtherDetail::getConsigneeRegion, req.getDistrict());
        uw.lambda().set(PresOtherDetail::getConsigneeDetailAddress, req.getAddr());
        boolean flag = presOtherDetailService.update(uw);
        if (!flag) {
            throw new ZekeException(400, "修改处方收货人信息失败");
        }
//        UpdateWrapper<PresOtherDetail> uw = new UpdateWrapper<>();
//        uw.lambda().eq(PresOtherDetail::getTencode, req.getTencode());
//        uw.lambda().set(PresOtherDetail::getReceive, req.getReceiver());
//        uw.lambda().set(PresOtherDetail::getReceivePhone, req.getMobileNumber());
//        uw.lambda().set(PresOtherDetail::getProvince, req.getProvince());
//        uw.lambda().set(PresOtherDetail::getCity, req.getCity());
//        uw.lambda().set(PresOtherDetail::getDistrict, req.getDistrict());
//        uw.lambda().set(PresOtherDetail::getAddr, req.getAddr());
//        boolean flag = presOtherDetailService.update(uw);
//        if (!flag) {
//            throw new ZekeException(400, "修改处方收货人信息失败");
//        }
    }
}
